<template>
  <div>
    <el-form
      label-width="120px"
      :model="skuInfo"
    >
      <el-form-item label="SPU名称">{{ spuInfo.spuName }}</el-form-item>
      <el-form-item label="SKU名称">
        <el-input
          placeholder="sku名称"
          v-model="skuInfo.skuName"
        ></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          v-model="skuInfo.price"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input
          placeholder="重量(千克)"
          v-model="skuInfo.weight"
        ></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          placeholder="规格描述"
          v-model="skuInfo.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form inline>
          <el-form-item
            style="margin-bottom: 10px"
            :label="attr.attrName"
            v-for="attr in attrInfoList"
            :key="attr.id"
          >
            <el-select
              placeholder="请选择"
              v-model="attr.attrIdAndValueId"
            >
              <el-option
                v-for="attrValue in attr.attrValueList"
                :label="attrValue.valueName"
                :key="attrValue.id"
                :value="`${attr.id}:${attrValue.id}`"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form inline>
          <el-form-item
            :label="saleAttr.saleAttrName"
            v-for="saleAttr in saleattrList"
            :key="saleAttr.id"
          >
            <el-select
              placeholder="请选择"
              v-model="saleAttr.saleAttrIdAndValueId"
            >
              <el-option
                v-for="saleAttrValue in saleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
                :label="saleAttrValue.saleAttrValueName"
                :value="`${saleAttr.id}:${saleAttrValue.id}`"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          ref="multipleTable"
          border
          tooltip-effect="dark"
          style="width: 100%"
          :data="spuImgList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          ></el-table-column>
          <el-table-column label="图片">
            <template scope="{row}">
              <div style="width: 1200px; height: 120px">
                <img
                  style="width: 120px; height: 120px border-radius: 8px;"
                  :src="row.imgUrl"
                  alt=""
                />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="名称"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                size="mini"
                v-if="row.isDefault === 0"
                @click="setDefault(row)"
              >设为默认</el-button>
              <el-button
                type="success"
                size="mini"
                v-else
                @click="row.isDefault = 0"
              >默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="saveSkuInfo"
        >保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      saleattrList: [], // 销售属性
      spuImgList: [],
      attrInfoList: [], // 平台属性
      // 保存表单
      skuInfo: {
        category3Id: 0,
        price: 0,
        // 平台属性
        skuAttrValueList: [
          {
            attrId: 0,
            valueId: 0,
          },
        ],
        skuDefaultImg: "",
        skuDesc: "",
        skuImageList: [
          {
            imgName: "",
            imgUrl: "",
            isDefault: "",
            spuImgId: 0,
          },
        ],
        skuName: "",
        // 销售属性
        skuSaleAttrValueList: [
          {
            saleAttrId: 0,
            saleAttrValueId: 0,
          },
        ],
        spuId: 0,
        tmId: 0,
        weight: "",
      },
      // 父组件传来的数据
      spuInfo: {},
      // 选中的图片列表
      selectImgList: []
    };
  },
  methods: {
    cancel() {
      this.$emit("cancel", 0);
      Object.assign(this._data, this.$options.data());
    },
    async getData(spu, categoryIdList) {
      this.spuInfo = spu;
      this.skuInfo.tmId = spu.tmId;
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.spuId = spu.id;
      // 获取销售属性数据
      const res = await this.$Api.sku.reqGetSpuSaleAttrList(spu.id);
      if (res.code == 200) {
        this.saleattrList = res.data;
      }
      // 获取图片数据
      const res1 = await this.$Api.sku.reqGetSpuImgList(spu.id);
      if (res1.code == 200) {
        let imgList = res1.data;
        imgList.forEach((item) => (item.isDefault = 0));
        this.spuImgList = imgList;
      }
      // 获取平台属性数据
      const res2 = await this.$Api.sku.reqGetAttrInfoList(
        categoryIdList.category1Id,
        categoryIdList.category2Id,
        categoryIdList.category3Id
      );
      if (res2.code == 200) {
        this.attrInfoList = res2.data;
      }
    },
    handleSelectionChange(selection) {
      this.selectImgList = selection
    },
    setDefault(row) {
      this.spuImgList.forEach((item) => (item.isDefault = 0));
      row.isDefault = 1;
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    async saveSkuInfo() {
      const { attrInfoList, skuInfo, saleattrList, selectImgList } = this
      //整理平台属的数据
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(":");
          prev.push({ attrId, valueId });
        }
        return prev;
      }, []);
      //整理销售属性
      skuInfo.skuSaleAttrValueList = saleattrList.reduce((prev, item) => {
        if (item.saleAttrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] = item.saleAttrIdAndValueId.split(':');
          prev.push({ saleAttrId, saleAttrValueId });
        }
        return prev;
      }, []);
      skuInfo.skuImageList = selectImgList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        }
      })
      try {
        const res = await this.$Api.sku.reqSaveSkuInfo(this.spuInfo)
        if (res.code == 200) {
          this.$message.success('保存成功')
          this.$emit("cancel", 0);
        }
      } catch (error) {
        this.$message.error('保存失败')
      }
    },
  },
};
</script>

<style></style>
